package br.com.gecandango.portlets.material;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import br.com.gecandango.beans.Contato;
import br.com.gecandango.beans.Especialidade;
import br.com.gecandango.beans.Etapa;
import br.com.gecandango.beans.FichaSaude;
import br.com.gecandango.beans.Material;
import br.com.gecandango.beans.Socio;

public class MaterialPortlet extends MaterialAction {

	static final Logger logger = Logger
	.getLogger(MaterialPortlet.class);
	
	public void processAction(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {
		String acao = req.getParameter("acao");
		Material material = new Material();
		if (acao.equals("add")){
			setForward(req, "portlet.ext.material.edit");
		} else if (acao.equals("list")){
			req.setAttribute("materiais", getMaterialService().list());
			setForward(req, "portlet.ext.material.list");
		} else if (acao.equals("edit")){
			material = getMaterialService().find(Long.parseLong(req
					.getParameter("id_material")));
			req.setAttribute("material", material);
			setForward(req, "portlet.ext.material.edit");
		} else if (acao.equals("delete")){
			material = getMaterialService().find(Long.parseLong(req
					.getParameter("id_material")));
			getMaterialService().delete(material);
			req.setAttribute("materiais", getMaterialService().list());
			setForward(req, "portlet.ext.material.list");
		} else if (acao.equals("salvar")){
			material = salvarMaterial(req);
			req.setAttribute("material", material);
			setForward(req, "portlet.ext.material.edit");
		} else {
			setForward(req, "portlet.ext.material.view");
		}
		
		
	}
	public Material salvarMaterial(ActionRequest request){
		String id = (String) request.getParameter("id_material");
		String nome = (String) request.getParameter("nome");
		String descricao = (String) request.getParameter("descricao");
		String quantidade = (String) request.getParameter("quantidade");
		Material material = new Material();
		
		if(id!= null){
			try {
				material.setId(Long.parseLong(id));
			} catch (Exception e) {
			}
		}
		material.setNome(nome);
		
		material.setDescricao(descricao);
		material.setQuantidade(quantidade);
		try {
			return getMaterialService().save(material);
		} catch (Exception e) {
			return null;
		}		
	}
}
